<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="积分变动" prop="points">
        <el-input
          v-model="queryParams.points"
          placeholder="请输入积分变动"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录ID" prop="relatedId">
        <el-input
          v-model="queryParams.relatedId"
          placeholder="请输入记录ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="变动说明" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入变动说明"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="当前积分" prop="pointsBalance">
        <el-input
          v-model="queryParams.pointsBalance"
          placeholder="请输入当前积分"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择记录时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['core:pointRecords:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['core:pointRecords:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['core:pointRecords:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['core:pointRecords:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pointRecordsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="积分变动记录ID" align="center" prop="id" />
      <el-table-column label="用户ID" align="center" prop="userId" />
      <el-table-column label="变动类型" align="center" prop="type">
        <template #default="scope">
          <el-tag :type="getTypeTagType(scope.row.type)">
            {{ getTypeText(scope.row.type) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="积分变动" align="center" prop="points" />
      <el-table-column label="记录ID" align="center" prop="relatedId" />
      <el-table-column label="变动说明" align="center" prop="description" />
      <el-table-column label="当前积分" align="center" prop="pointsBalance" />
      <el-table-column label="记录时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['core:pointRecords:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['core:pointRecords:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改积分变动明细对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="pointRecordsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="变动类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择变动类型">
            <el-option label="签到" :value="1" />
            <el-option label="兑换" :value="2" />
            <el-option label="抽奖" :value="3" />
            <el-option label="系统调整" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="积分变动" prop="points">
          <el-input v-model="form.points" placeholder="请输入积分变动" />
        </el-form-item>
        <el-form-item label="记录ID" prop="relatedId">
          <el-input v-model="form.relatedId" placeholder="请输入记录ID" />
        </el-form-item>
        <el-form-item label="变动说明" prop="description">
          <el-input v-model="form.description" placeholder="请输入变动说明" />
        </el-form-item>
        <el-form-item label="当前积分" prop="pointsBalance">
          <el-input v-model="form.pointsBalance" placeholder="请输入当前积分" />
        </el-form-item>
        <el-form-item label="记录时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择记录时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PointRecords">
import { listPointRecords, getPointRecords, delPointRecords, addPointRecords, updatePointRecords } from "@/api/core/pointRecords"

const { proxy } = getCurrentInstance()

const pointRecordsList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userId: null,
    type: null,
    points: null,
    relatedId: null,
    description: null,
    pointsBalance: null,
    createdAt: null
  },
  rules: {
    userId: [
      { required: true, message: "用户ID不能为空", trigger: "blur" }
    ],
    type: [
      { required: true, message: "变动类型不能为空", trigger: "change" }
    ],
    points: [
      { required: true, message: "积分变动不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

// 获取变动类型文本
function getTypeText(type) {
  const typeMap = {
    1: '签到',
    2: '兑换',
    3: '抽奖',
    4: '系统调整'
  }
  return typeMap[type] || '未知'
}

// 获取变动类型标签样式
function getTypeTagType(type) {
  const typeTagMap = {
    1: 'success',   // 签到 - 绿色
    2: 'warning',   // 兑换 - 橙色
    3: 'info',      // 抽奖 - 蓝色
    4: 'danger'     // 系统调整 - 红色
  }
  return typeTagMap[type] || 'info'
}

/** 查询积分变动明细列表 */
function getList() {
  loading.value = true
  listPointRecords(queryParams.value).then(response => {
    pointRecordsList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    type: 1,
    points: null,
    relatedId: null,
    description: null,
    pointsBalance: null,
    createdAt: null
  }
  proxy.resetForm("pointRecordsRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加积分变动明细"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getPointRecords(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改积分变动明细"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["pointRecordsRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePointRecords(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addPointRecords(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除积分变动明细编号为"' + _ids + '"的数据项？').then(function() {
    return delPointRecords(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('core/pointRecords/export', {
    ...queryParams.value
  }, `pointRecords_${new Date().getTime()}.xlsx`)
}

getList()
</script>
